package com.acme.entity.security;

import java.util.Collection;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import com.acme.controller.HomeController;
import com.acme.dao.security.DBUserDAO;
import com.acme.entity.User;

@Component
public class DBUserDetails implements UserDetails
	{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

	private User user;
	
	private DBUserDAO dbUserDAO;

	public DBUserDetails(User user)	{
		this.user = user;
	}
	
	public DBUserDetails() {}
	
	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
	return dbUserDAO.getPermissionsByUser(user);
	}
	
	public void setUser(User user)
		{
		logger.info("User " + user.getUsername() + " was set.");
		logger.info(this.toString());
		this.user = user;
		}

	public void setDbUserDAO(DBUserDAO dbUserDAO)
		{
		this.dbUserDAO = dbUserDAO;
		}

	@Override
	public String getPassword() {
		return user.getPassword();
	}

	@Override
	public String getUsername() {
		return user.getUsername();
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return user.isEnabled();
	}
}
